<%@ page contentType="text/html; charset=UTF-8" %>
<script type="text/javascript" src="<%=session.getServletContext().getContextPath() %>/js/jquery.form.js"></script>
		
<script type="text/javascript">

    $(function () {
        
    	$('#villagerCreate').click(function(){
        	ajaxSubmitFormSimple($('#collapse'),'waterBillManage_createVillagerSave.action',$('#formDefault').serialize(),false);
        });
    	
    	$('#transactionCreate').click(function(){
    		if(confirm('ยืนยันการกรอกข้อมูล')) {
	    		document.getElementById('MZone').disabled = false;
	        	document.getElementById('MYear').disabled = false;
	        	document.getElementById('MMonth').disabled = false;
	        	ajaxSubmitFormSimple($('#collapse'),'waterBillManage_createTransactionSave.action',$('#formDefault').serialize(),false);
	        	document.getElementById('MZone').disabled = true;
	        	document.getElementById('MYear').disabled = true;
	        	document.getElementById('MMonth').disabled = true;
    		}else {
        		return false;
        	}
        });
    	
        $('#waterBillSearch').click(function(){
        	
        	if(isValidationField('formDefault')) {
        		ajaxSubmitFormSimple($('#collapse'),'waterBillManage_searchWaterBill.action',$('#formDefault').serialize(),false);
        	}
        	
        });
        
        $('#villagerSearch').click(function(){
        	ajaxSubmitFormSimple($('#divSearch'),'waterBillManage_searchVillager.action',$('#formDefault').serialize(),false);
        });
        
        $('#waterBillPrint').click(function(){
        	ajaxSubmitFormSimple($('#collapse'),'waterBillManage_printWaterBill.action',$('#formDefault').serialize(),false);
        }); 
        
        $('#waterBillCreateReset').click(function(){
        	window.location.reload(true);
        });
        
        $('#waterBillCreateSave').click(function(){
        	if(confirm('ยืนยันการกรอกข้อมูล')) {
        		generateOrder();
        		document.getElementById('MZone').disabled = false;
            	document.getElementById('MYear').disabled = false;
            	document.getElementById('MMonth').disabled = false;
            	//alert($('#formDefault').serialize());
            	ajaxSubmitFormSimple($('#collapse'),'waterBillManage_createWaterBillSave.action',$('#formDefault').serialize(),false);
        		document.getElementById('MZone').disabled = true;
            	document.getElementById('MYear').disabled = true;
            	document.getElementById('MMonth').disabled = true;
        	}else {
        		return false;
        	}
        	
        });
        
        $('#waterBillUpdateSave').click(function(){
        	if(confirm('ยืนยันการกรอกข้อมูล')) {
        		generateOrder();
        		document.getElementById('MZone').disabled = false;
            	document.getElementById('MYear').disabled = false;
            	document.getElementById('MMonth').disabled = false;
            	//alert($('#formDefault').serialize());
            	ajaxSubmitFormSimple($('#collapse'),'waterBillManage_updateWaterBillSave.action',$('#formDefault').serialize(),false);
        		document.getElementById('MZone').disabled = true;
            	document.getElementById('MYear').disabled = true;
            	document.getElementById('MMonth').disabled = true;
        	}else {
        		return false;
        	}
        	
        });
        
        $('#waterBillPrintSave').click(function(){
        	document.getElementById('MZone').disabled = false;
        	document.getElementById('MYear').disabled = false;
        	document.getElementById('MMonth').disabled = false;
        	//alert($('#formDefault').serialize());
        	$('#formDefault').submit();

        	document.getElementById('MZone').disabled = true;
        	document.getElementById('MYear').disabled = true;
        	document.getElementById('MMonth').disabled = true;
        	
        });
        
        $('#waterBillReportSave').click(function(){
        	
        	$('#formDefault').submit();
        	
        });
        
		$('#lekyerReportSave').click(function(){
        	
        	$('#formDefault').submit();
        	
        });
        
        $('#villagerEditSave').click(function(){
        	ajaxSubmitFormSimple($('#collapse'),'waterBillManage_villagerEditSave.action',$('#formDefault').serialize(),false);
        	
        });
        
        $('#waterBillCreate').click(function(){
        	ajaxSubmitFormSimple($('#collapse'),'waterBillManage_createWaterBillDetail.action',$('#formDefault').serialize(),false);
        	
        });
        
        $('#waterBillTransactionInitCreate').click(function(){
        	ajaxSubmitFormSimple($('#collapse'),'waterBillManage_createTransaction.action',$('#formDefault').serialize(),false);
        	
        });
        
        $('.datepicker').datepicker({
      	    dateFormat: 'dd/mm/yy',
      	  	firstDay: 1,
      	  	showOtherMonths: true,
			changeYear: true ,
			changeMonth: true ,
	        isBE: true,        
	        autoConversionField: false ,
	        monthNames: ['มกราคม','กุมภาพันธ์','มีนาคม','เมษายน','พฤษภาคม','มิถุนายน',
	        	'กรกฎาคม','สิงหาคม','กันยายน','ตุลาคม','พฤศจิกายน','ธันวาคม'],
	        monthNamesShort: ['ม.ค.','ก.พ.','มี.ค.','เม.ย.','พ.ค.','มิ.ย.',
	        	'ก.ค.','ส.ค.','ก.ย.','ต.ค.','พ.ย.','ธ.ค.'],
	        dayNames: ['อาทิตย์','จันทร์','อังคาร','พุธ','พฤหัสบดี','ศุกร์','เสาร์'],
	        dayNamesShort: ['อา.','จ.','อ.','พ.','พฤ.','ศ.','ส.'],
	        dayNamesMin: ['อา.','จ.','อ.','พ.','พฤ.','ศ.','ส.']
		});
    });
    
    function villagerEdit(val) {
    	$('#villMeterNumber').val(val);
    	ajaxSubmitFormSimple($('#collapse'),'waterBillManage_villagerEditInit.action',$('#formDefault').serialize(),false);
    }
    
    function villagerDelete(val) {
    	if(confirm('ยืนยันการลบข้อมูล')) {
    		$('#villMeterNumber').val(val);
    		ajaxSubmitFormSimple($('#collapse'),'waterBillManage_villagerDelete.action',$('#formDefault').serialize(),false);
    	}
    	
    }
    
    function moveNextFocus(next_id,_key) {
		//alert(next_id);
		var indexName = next_id.substring(0,next_id.lastIndexOf("_")+1);
		//alert('indexName='+indexName);
		var indexKey = parseInt(next_id.substring(next_id.lastIndexOf("_")+1,next_id.length))
		//alert('indexKey='+indexKey);
		var next_ctrl = indexName + (indexKey+1);
		//alert('next_ctrl='+next_ctrl);
            if (_key == 13) {
                var _next = document.getElementById(next_ctrl);
                _next.focus();
                return false;
            }
    }
    
    function generateOrder(){
    	/* var mOrder = document.getElementsByName('MOrder');
    	if(mOrder != null) {
    		var firstNum = parseInt(mOrder[0].value);
    		if(firstNum < 1) {
    			alert('ไม่สามารถเรียงลำดับได้');
    			return;
    		}
    		for(var i=1; i <= mOrder.length; i++) {
    			firstNum = firstNum+1;
    			mOrder[i].value = firstNum;
    		}
    		
    	} */ 
    }
    function generateBook(){
    	var max = 50;
    	var mBookId = document.getElementsByName('MBookId');
    	var mBookNum = document.getElementsByName('MBookNum');
    	if(mBookId != null && mBookNum != null) {
    		var bookId = parseInt(mBookId[0].value);
    		var bookNum = parseInt(mBookNum[0].value);
    		//alert('bookId='+bookId+', bookNum='+bookNum);
    		if(bookId < 1 || bookNum < 1 ) {
    			alert('ไม่สามารถเรียงลำดับได้');
    			return;
    		}else if(bookNum > max) {
    			alert('ไม่สามารถเรียงลำดับได้');
    			return;
    		}
    		for(var i=1; i <= mBookId.length; i++) {
    			if(bookNum == max) {
    				bookId = bookId+1;
    				bookNum = 1;
    				mBookId[i].value = bookId;
    				mBookNum[i].value = bookNum;
    			}else {
    				bookNum = bookNum+1;
    				mBookId[i].value = bookId;
    				mBookNum[i].value = bookNum;
    			}
    		}
    	}
    }
    
    function deleteTrans(val) {
    	var valSplit = val.split("||");
    	if(confirm('ยืนยันการลบข้อมูล\n\nมิเตอร์  : '+valSplit[3]+'\nชื่อ '+valSplit[4])) {
    		document.getElementById('MZone').disabled = false;
        	document.getElementById('MYear').disabled = false;
        	document.getElementById('MMonth').disabled = false;
        	document.getElementById('MMeter').value = valSplit[3];
        	//alert($('#formDefault').serialize());
        	ajaxSubmitFormSimple($('#collapse'),'waterBillManage_deleteTransaction.action',$('#formDefault').serialize(),false);
    		document.getElementById('MZone').disabled = true;
        	document.getElementById('MYear').disabled = true;
        	document.getElementById('MMonth').disabled = true;
    	}
    	
    }
 
</script>